function MonthSelector ()
{
	this.nome = "nome del mese";
	this.prefix = 'cal';
	this._giorni = [ 0, 0, 0, 0, 0, 0, 0 ];
	this._uscite = [];
	this._curr_month = 0;
	this._curr_year = 0;
	/* 
	   fuction add ( d ) 
		param: d	- date to be added ( either string or object format )
	   it will add the given date to the calendar
	*/

	this.add = function ( d )
	{
		// if ( typeof d == "string" )
		d = new Date ( d );

		this._uscite.push ( d );
		this._giorni [ d.getDay () ] = 1;
		this._curr_month = d.getMonth ()+1; // javascript date object start from 0
		this._curr_year = d.getFullYear ();
	};

	/* 
	   fuction toString () 
		param: none 
	   it will output the formatted calendar 
	*/
	this.render = function ()
	{
		var s = new String.buffer ();
		var giorni = this._count_days ();


		s.add ( '<table class="mese_selector">' );
		s.add ( '<tr class="mese_name"><td colspan="' + giorni + '" class="name">' + this.nome + ' ' + this._curr_year + '</td></tr>' );
		s.add ( this._create_headers () );

		s.add ( this._create_days ( giorni ) ); // passo il numero di giorni 

		s.add ( '</table>' );

		return s.toString ();
	};

	/* 
	   fuction _count_days () 
		param: none 
	   it will count how many days in a week
	*/

	this._count_days = function ()
	{
		var t, l = 7, count = 0;

		for ( t = 0; t < l; t ++ )
			if ( this._giorni [ t ] ) count += 1;

		return count;
	};

	/* 
	   fuction _create_headers () 
		param: none 
	   it will format table's headers and it will add javascript function to check all
	   relative checkboxes
	*/
	this._create_headers = function ()
	{
		var s = new String.buffer ();
		var chk_id = new String.buffer ();
		var days = [ "Dom", "Lun", "Mar", "Mer", "Gio", "Ven", "Sab" ];
		var t, l = 7;


		s.add ( '<tr class="giorni">' );

		for ( t = 0; t < l; t ++ )
			if ( this._giorni [ t ] ) 
			{
				chk_id.add ( this.prefix + "_" + this._curr_month + "_" + t.toString () );
				id = chk_id.toString ();
				s.add ( '<td class="giorno"><input type="checkbox" id="' + id +'" onclick="month_check_all( \''+ this.prefix + '\', ' + this._curr_month +', ' + t + ', \'' + id + '\' );" />' + days [ t ] + '</td>' );
				chk_id.clear();
			}
		s.add ( "</tr>" );

		return s.toString ();
	};

	this._get_next_day = function ( curr_day )
	{
		var t, l = 7;

		if ( typeof ( curr_day ) == 'undefined' ) curr_day = -1;

		for ( t = curr_day + 1; t < l; t ++ )
			if ( this._giorni [ t ] ) return t;

		for ( t = 0; t < l; t ++ )
			if ( this._giorni [ t ] ) return t;

		return 0;
	};
	var day_col = [ 'col_0', 'col_1' ];
	var _class = "";
	this._create_days = function ( giorni )
	{
		var s = new String.buffer ();
		var t, l = this._uscite.length, u;
		var is_open = true;
		var next_day = this._get_next_day ();
		var curr_day;
		// checkbox id
		var chk_id = new String.buffer ();
	

		s.add ( '<tr class="even">' );
		var even_odd = 0;
		for ( t = 0; t < l; t ++ )
		{
			u = this._uscite [ t ];
			curr_day = u.getDay ();
			curr_date = u.getDate ();

			if ( ! is_open )
			{
				even_odd = 1- even_odd
				is_open = true;
				if ( even_odd == 0 )
				{
					s.add ( '<tr class="even">' );
				}	
				else
				{
					s.add ( '<tr class="odd">' );
				}
				
			}

			while ( curr_day != next_day )
			{
				s.add ( "<td></td>" )
				next_day = this._get_next_day ( next_day );
			}
			/* checkbox id will be 
				prefix, current month, current day (of the week) , current date (of the month) separated by underscore
				this way we can get all the day of same month on the same week day
			*/

			
			chk_id.add ( this.prefix + "_" + this._curr_month + "_" + curr_day + "_" + curr_date );

			_class = day_col [ curr_day % 2 ]; 
			s.add ( '<td class="' + _class + '"><input id="' );
			s.add ( chk_id.toString() );
			s.add ( '" value = "' );
			s.add ( curr_date + '/' + this._curr_month + '/' + this._curr_year );
			s.add ( '" type="checkbox" />' + curr_date + '</td>' );
			next_day = this._get_next_day ( next_day );
			
			if ( next_day < curr_day || giorni == 1 ) // se giorni == 1 allora crea un'unica colonna ( chiude sempre <tr> )
			{
				s.add ( '</tr>' );
				is_open = false;
			}

			chk_id.clear ();
		}

		if ( is_open ) s.add ( "</tr>" );

		return s.toString ();
	};
	// funzione clear per tornare ai valori iniziali 
	this.clear = function ()
	{
		this.nome = "nome del mese";

		this._giorni = [ 0, 0, 0, 0, 0, 0, 0 ];
		this._uscite = [];
	};
}

/* 
   fuction month_check_all ( prefix, month, day, id ) 
	param: prefix	- current checkboxes prefix
	param: month	- current month to be selected
	param: day	- day of the week of current month to be selected
	param: id	- checkbox id
   it will check/uncheck all selceted days of given month
*/
function month_check_all ( prefix, month, day, id )
{	
	var temp = new String.buffer ();
	var cond = true;
	if ( $( id ) ) 
		cond = $( id ).checked;

	for (d = 1; d<32 ; d++ )
	{
		temp.add ( prefix + "_" + month + "_" + day + "_" + d  );

		if ( $( temp.toString () ) ) 
			$( temp.toString () ).checked = cond;
		temp.clear ();
	}
};
